<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Musaids extends CI_Controller {

    var $musaid_id;
    var $status;
    var $cari;
    var $message;

    function __construct() {
        parent::__construct();
        $this->load->library('auth');
        $this->auth->restrict();

        $this->load->helper('text');
        $this->load->model('m_musaid');
        $uri = $this->uri->uri_to_assoc(3);

        if (isset($uri['id'])) {
            $this->musaid_id = $uri['id'];
        }

        if (isset($uri['status'])) {
            $this->status = $uri['status'];
        }

        if (isset($_POST['in_cari'])) {
            $this->cari = $this->input->post('in_cari');
            $this->session->set_userdata('cari', $this->cari);
        }

        if ($this->session->userdata('cari')) {
            $this->cari = $this->session->userdata('cari');
        }

        if ($this->auth->cek_role_member() != '1') {
            redirect('homes');
        }
    }

    public function index($message = NULL) {
        $data['message'] = $message;
        //pengaturan untuk pagination
        $this->m_musaid->cari = $this->cari;

        $config['base_url'] = base_url() . 'index.php/musaids/index/page/';
        $config['total_rows'] = $this->m_musaid->get_row_all();
        $config['per_page'] = '20';

        $config['first_link'] = $this->lang->line('paging_first');
        $config['first_tag_open'] = '<li class="prev page">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = $this->lang->line('paging_last');
        $config['last_tag_open'] = '<li class="next page">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = $this->lang->line('paging_next');
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = $this->lang->line('paging_prev');
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';

        $config['cur_tag_open'] = '<li class="active"><a href="">';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';

        $config['uri_segment'] = 4;
        $this->m_musaid->num_pages = $config['per_page'];
        $this->m_musaid->offset = $this->uri->segment(4);
        $this->m_musaid->paginate = TRUE;
        $this->pagination->initialize($config);

        $musaids = $this->m_musaid->get();
        //$data = $this->auth->load_style();
//        $data['sm_musaid'] = 'active';
        $data['title'] = $this->lang->line('menu_musaid') . ' ' . $this->lang->line('webname');
        $data['musaids'] = $musaids;
        $data['paging'] = $this->pagination->create_links();
        $data['total'] = $config['total_rows'];

        if (!$this->uri->segment(4)) {
            $data['no_page'] = 0;
        } else {
            $data['no_page'] = $this->uri->segment(4);
        }

        $this->load->view('view_header', $data);
        $this->load->view('musaid_view', $data);
        $this->load->view('view_footer', $data);
    }

    public function view() {
        $this->session->unset_userdata('cari');

        redirect('musaids/');
    }

    public function add($message = NULL) {
        $this->form_validation->set_error_delimiters('<div class="alert alert-warning">', '</div>');
        $this->form_validation->set_rules('in_nama', $this->lang->line('nama'), 'trim|required|xss_clean|callback__musaids_name_check');
        $this->form_validation->set_rules('in_kelamin', $this->lang->line('kelamin'), 'trim|required');
        $this->form_validation->set_rules('in_status', $this->lang->line('status'), 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $musaids_kelamin = $this->auth->load_kelamin();
            $aktifs = array($this->lang->line('nonaktif'), $this->lang->line('aktif'));

            $data['title'] = $this->lang->line('menu_musaid') . ' ' . $this->lang->line('webname');
            $data['musaid_id'] = $this->musaid_id;
            $data['aktifs'] = $aktifs;
            $data['musaids_kelamin'] = $musaids_kelamin;
            $data['message'] = $message;

            $this->load->view('view_header', $data);
            $this->load->view('musaid_form', $data);
            $this->load->view('view_footer', $data);
        } else {
            $this->m_musaid->musaid_nama = $this->input->post('in_nama');
            $this->m_musaid->musaid_kelamin = $this->input->post('in_kelamin');
            $this->m_musaid->musaid_hp = $this->input->post('in_hp');
            $this->m_musaid->musaid_alamat = $this->input->post('in_alamat');
            $this->m_musaid->musaid_status = $this->input->post('in_status');
            $this->m_musaid->musaid_tanggal = date('Y-m-d H:i:s', strtotime($this->input->post('in_tanggal'))); //date('Y-m-d H:i:s');

            $this->m_musaid->musaid_oleh = $this->auth->get_member_by_idnya($this->session->userdata('member_id'));
            $this->m_musaid->musaid_dirubah = date('Y-m-d H:i:s');

            $this->m_musaid->musaid_id = '';
            $this->m_musaid->save();

            $this->view();
            //redirect('musaids/');
        }
    }

    public function edit() {
        $this->form_validation->set_error_delimiters('<div class="alert alert-warning">', '</div>');
        $this->form_validation->set_rules('in_nama', $this->lang->line('nama'), 'trim|required|xss_clean');
        $this->form_validation->set_rules('in_kelamin', $this->lang->line('kelamin'), 'trim|required');
        $this->form_validation->set_rules('in_status', $this->lang->line('status'), 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $musaids_kelamin = $this->auth->load_kelamin();
            $aktifs = array($this->lang->line('nonaktif'), $this->lang->line('aktif'));

            $data['title'] = $this->lang->line('menu_musaid') . ' ' . $this->lang->line('webname');
            $data['musaid_id'] = $this->musaid_id;
            $data['aktifs'] = $aktifs;
            $data['musaids_kelamin'] = $musaids_kelamin;
            $data['message'] = $this->message;
            $data['musaids'] = $this->m_musaid->get_by_id($this->musaid_id);

            $this->load->view('view_header', $data);
            $this->load->view('musaid_form', $data);
            $this->load->view('view_footer', $data);
        } else {
            $this->m_musaid->musaid_nama = $this->input->post('in_nama');
            $this->m_musaid->musaid_kelamin = $this->input->post('in_kelamin');
            $this->m_musaid->musaid_hp = $this->input->post('in_hp');
            $this->m_musaid->musaid_alamat = $this->input->post('in_alamat');
            $this->m_musaid->musaid_status = $this->input->post('in_status');
            //$this->m_musaid->musaid_tanggal = date('Y-m-d H:i:s');
            $this->m_musaid->musaid_tanggal = date('Y-m-d H:i:s', strtotime($this->input->post('in_tanggal')));

            $this->m_musaid->musaid_oleh = $this->auth->get_member_by_idnya($this->session->userdata('member_id'));
            $this->m_musaid->musaid_dirubah = date('Y-m-d H:i:s');

            $this->m_musaid->musaid_id = $this->musaid_id;
            $this->m_musaid->save();
            //echo 'kelamin ' . $this->m_musaid->musaid_kelamin;
            $this->view();
        }
    }

    public function delete() {
        $this->m_musaid->musaid_id = $this->musaid_id;
        $this->m_musaid->delete();

        $this->view();
    }

    public function _musaids_name_check($str) {
        if ($this->m_musaid->get_musaid_name($str)) {
            $this->form_validation->set_message('_musaids_name_check', 'Nama pengguna " ' . $str . ' " sudah digunakan.');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function musaids_status() {
        $this->m_musaid->musaid_id = $this->musaid_id;
        $this->m_musaid->musaid_status = $this->status;
        $this->m_musaid->set_status();

        redirect('musaids/view/');
    }

}

/* End of file musaids.php */
/* Location: ./application/controllers/musaids.php */